﻿/*
 * ContractWapper
 * 
 * Author: snowdreamist
 * Date: 2011/11/24
 * 
 */

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Reflection;
using BitTorrentSharp.KRPC.Messages;

namespace BitTorrentSharp.KRPC.Contract
{
    public class ServiceContractWrapper
    {
        private List<OperationContractWrapper> _operationWrappers;

        internal ServiceContractWrapper(Type contractType)
        {
            if (contractType == null)
                throw new ArgumentNullException("contractType");
            this.ServiceType = contractType;
            this.LoadContract(contractType);
        }

        #region properties
        /// <summary>
        /// Instance mode
        /// </summary>
        public ServiceInstanceMode InstanceMode { get; private set; }
        /// <summary>
        /// Custom factory
        /// </summary>
        public Type CustomInstanceFactory { get; private set; }
        /// <summary>
        /// Service type
        /// </summary>
        public Type ServiceType { get; private set; }

        #endregion

        #region private
        /// <summary>
        /// Load contract type
        /// </summary>
        /// <param name="contractType">service contract type</param>
        private void LoadContract(Type contractType)
        {
            // service contract
            object[] attrs = contractType.GetCustomAttributes(typeof(KRPCServiceContractAttribute), false);
            if (attrs == null || attrs.Length == 0)
                throw new KRPCContractLoadException("ServiceContract must attach attribute [KRPCServiceContract]");
            if (attrs.Length > 1)
                throw new KRPCContractLoadException("Only one [ServiceContract] attribute is allowed");
            KRPCServiceContractAttribute serviceContractAttr = attrs[0] as KRPCServiceContractAttribute;

            // basic
            this.InstanceMode = serviceContractAttr.InstanceMode;
            this.CustomInstanceFactory = serviceContractAttr.CustomInstanceFactory;
        }

        #endregion
    }
}
